/**
 *
 */
package com.edooschool.webpay.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo2do.core.entity.exception.BusinessException;
import com.edooschool.webpay.entity.prepare.PrepareRequest;
import com.edooschool.webpay.entity.prepare.PrepareResponse;
import com.edooschool.webpay.handler.PaymentFacade;

/**
 * @author downpour
 *
 */
@Controller
@RequestMapping("")
public class PaymentPrepareController {

    private static final Logger logger = LoggerFactory.getLogger(PaymentPrepareController.class);

    @Autowired
    private PaymentFacade paymentFacade;

    /**
     *
     * @param prepareRequest
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/prepare", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody PrepareResponse prepare(@RequestBody PrepareRequest prepareRequest,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response) {

        PrepareResponse prepareResponse = new PrepareResponse();

        logger.trace("#prepare() - prepare request [ orderNo: {}, requestNo: {}, userId: {}, channel: {}, amount: {}, subject: {}, body: {}] ... ",
                     prepareRequest.getOrderNo(), prepareRequest.getRequestNo(), prepareRequest.getUserId(), prepareRequest.getChannel(), prepareRequest.getAmount(), prepareRequest.getSubject(), prepareRequest.getBody());

        try {
            
            if(prepareRequest.isValid()) {  // validate payment request

                // 1. prepare payment parameters for client
                prepareResponse = paymentFacade.prepare(prepareRequest);
            
            } else {
                
                // let's treat this as a 400 error
                prepareResponse.fail("400").message("支付参数有误");
                response.setStatus(400);
            
            }
            
        } catch (BusinessException e) {
            
            logger.error("#prepare - error occurs when create payment context for order_no[{}], let's stop the payment process.", prepareRequest.getOrderNo(), e);
            
            prepareResponse.fail("500").message(e.getMessage());
            response.setStatus(500);
        
        } catch (Exception e) {
            
            logger.error("#prepare - error occurs when create payment context for order_no[{}], let's stop the payment process.", prepareRequest.getOrderNo(), e);
            
            prepareResponse.fail("500").message("创建本地支付环境失败");
            response.setStatus(500);
        
        }
        
        return prepareResponse;
    }

}
